package com.tt.utils.wordUtils;

import cn.hutool.json.JSONUtil;
import cn.hutool.poi.excel.style.Align;
import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.config.Configure;
import com.deepoove.poi.config.ConfigureBuilder;
import com.deepoove.poi.data.*;
import com.deepoove.poi.data.style.CellStyle;
import com.deepoove.poi.data.style.ParagraphStyle;
import com.deepoove.poi.plugin.table.LoopRowTableRenderPolicy;
import com.deepoove.poi.plugin.table.MultipleRowTableRenderPolicy;
import com.deepoove.poi.plugin.table.SectionColumnTableRenderPolicy;
import com.deepoove.poi.policy.TableRenderPolicy;
import com.deepoove.poi.policy.TextRenderPolicy;
import com.deepoove.poi.util.TableTools;
import com.tt.utils.wordUtils.entity.*;
import com.tt.utils.wordUtils.renderplicy.*;
import lombok.SneakyThrows;
import org.apache.poi.ss.formula.functions.T;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Title: WordBaseUtils
 * @Author zl
 * @Package com.oberyun.goHAZOP.utils.wordUtils
 * @Date 2025/9/8 11:35
 * @description: 导出word工具类
 */

public class ExportWordUtilsV2 {




    public static void export(InputStream templatePath, String outputPath, WordBaseData wordBaseData) throws Exception {
        Map<String,Object> maps = new HashMap<>();
        ConfigureBuilder config = Configure.builder();
        //判断是否有文本
        if(wordBaseData.getTextContents() != null){
            for (TextContent textContent : wordBaseData.getTextContents()) {
                config.bind(textContent.getKey(), new TextRenderPolicy());
                maps.put(textContent.getKey(),textContent.getContent());
            }
        }
        //判断是否有图片
        if(wordBaseData.getImageContents() != null){
            for (ImageContent imageContent : wordBaseData.getImageContents()) {
                config.bind(imageContent.getKey(),new ImageRenderPolicy());
                maps.put(imageContent.getKey(),imageContent);
            }
        }
        //判断是否有表格
        if(wordBaseData.getTableContents() != null){
            for (TableContent tableContent : wordBaseData.getTableContents()) {
                config.bind(tableContent.getKey(), new TableRenderV2Policy());
//                config.bind(tableContent.getKey(), new TableRenderPolicy());
                maps.put(tableContent.getKey(),tableContent.getTableBody());
            }
        }
        //判断是否有超链接
        if(wordBaseData.getLinkContents() != null){
            for (LinkContent linkContent : wordBaseData.getLinkContents()) {
                config.bind(linkContent.getKey(),new HyperlinkRenderPolicy());
                maps.put(linkContent.getKey(),linkContent);
            }
        }
        Configure build = config.build();
        XWPFTemplate template = XWPFTemplate.compile(templatePath, build).render(maps);
        try (FileOutputStream out = new FileOutputStream(outputPath)) {
            template.write(out);
        }
        template.close();
    }

    public static WordBaseData sampleData() {
        WordBaseData wordBaseData = new WordBaseData();
        wordBaseData.setTextContents(Arrays.asList(new TextContent("name","张三")));
        TableContent.TableBody tableBody = new TableContent.TableBody();
        tableBody.setTitles(Arrays.asList(
                new TableContent.TableTitleData("序号"),
                new TableContent.TableTitleData("姓名"),
                new TableContent.TableTitleData("性别")
        ));
        tableBody.setBody(Arrays.asList(
                Arrays.asList(
                        new TableContent.TableCellData("1",0),
                        new TableContent.TableCellData("张三",0),
                        new TableContent.TableCellData("男",0)
                ),
                Arrays.asList(
                        new TableContent.TableCellData("2",0),
                        new TableContent.TableCellData("张三",0),
                        new TableContent.TableCellData("男",0)
                )
        ));
        tableBody.setColumnCount(3);
        wordBaseData.setTableContents(Arrays.asList(new TableContent("table",tableBody),
                new TableContent("table2",tableBody),
                new TableContent("table3",tableBody)));

        return wordBaseData;
    }

    @SneakyThrows
    public static void main(String[] args) {
        String templatePath = "E:\\1\\123.docx";
        String outputPath = "E:\\1\\output.docx";
//        export(new FileInputStream(templatePath), outputPath, sampleData());
//        System.out.println("导出成功：" + outputPath);
        Map<String, Object> data = new HashMap<>();
        Configure config = Configure.builder()
                .bind("rowData", new TableRenderV3Policy())
                .bind("rowDataV3", new TableRenderPolicy())
                .bind("rowDataV2", new MultipleRowTableRenderPolicy("[","]"))
                .build();
        // 数据格式：二维列表
        TableRenderData tableRenderData = Tables.of(
                Rows.of("name", "age", "dept").center().create(),
                Rows.of("张三", "25", "技术部").center().create(),
                Rows.of("李四", "30", "市场部").center().create(),
                Rows.of("王五", "28", "财务部").center().create()
        ).create();

        TableRenderData tableRenderData1 = new TableRenderData();
        RowRenderData rowRenderData = new RowRenderData();
        RowRenderData rowRenderData2 = new RowRenderData();
        ParagraphRenderData paragraphRenderData1 = new ParagraphRenderData();
        List<RenderData> contents = Arrays.asList(
                new TextRenderData("张三"),
                new TextRenderData("25"),
                new TextRenderData("技术部")
        );
        paragraphRenderData1.setContents(contents);

        ParagraphStyle paragraphStyle = new ParagraphStyle();
        paragraphStyle.setBackgroundColor("000000");
        paragraphRenderData1.setParagraphStyle(paragraphStyle);
        ParagraphRenderData paragraphRenderData2 = new ParagraphRenderData();
        paragraphRenderData2.addPicture(Pictures.of("C:\\Users\\Administrator\\Desktop\\照片\\1.png").size(100, 100).create());
        paragraphRenderData2.addText("asdfasdf");
        paragraphRenderData2.addText("asdfasd2f");
        rowRenderData.addCell(new CellRenderData().addParagraph(paragraphRenderData1));
        rowRenderData2.addCell(new CellRenderData().addParagraph(paragraphRenderData2));
        tableRenderData1.addRow(rowRenderData);
        tableRenderData1.addRow(rowRenderData2);


        // 数据格式
        List<Map<String, Object>> rowData = Arrays.asList(
                Map.of("name", "张三", "age", 25, "dept", "技术部"),
                Map.of("name", "李四", "age", 30, "dept", "市场部"),
                Map.of("name", "王五", "age", 28, "dept", "财务部")
        );
        data.put("rowDataV3", tableRenderData1);
        data.put("rowData", rowData);


        XWPFTemplate template = XWPFTemplate.compile(templatePath, config).render(data);

        try (FileOutputStream out = new FileOutputStream(outputPath)) {
            template.write(out);
        }
        template.close();
    }



}
